/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package customers;

import avenue.MyWindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import model.Customer;
import model.DBManager;
import model.Marka;
import model.Vehicle;
import model.Card;

/**
 *
 * @author Aggelos
 */
public class VehicleDialog extends javax.swing.JFrame
{

    EntityManager em;
    Customer custFromEditClientFrom;
    Vehicle vehicleFromEditClientForm;
    Card cardFromEditClientForm;
    String[] coloursArr  ={"Κοκκινο", "Ασπρο", "Μαυρο", "Καταμαυρο", "κεραμιδοκανελοκοραλλοκόκκινο", "σοκοραλλί - (σοκολατί - κοραλλί)"};
    String[] markesArr = {"Yugo", "DCV", "LADA", "WV", "Trabant", "Honda", "Yamaha"};     

    /**
     * Creates new form VehicleDialog
     */
    public VehicleDialog(Vehicle v)
    {
        // super(parent, modal);
        em = DBManager.em;
        vehicleFromEditClientForm = v;
        if (!em.getTransaction().isActive())
          {
            em.getTransaction().begin();
          }
        initComponents();
    }

    public VehicleDialog(Vehicle v, int customerID)
    {
        // super(parent, modal);
        em = DBManager.em;
        vehicleFromEditClientForm = v;
        if (!em.getTransaction().isActive())
          {
            em.getTransaction().begin();
          }
        initComponents();
    }

    public VehicleDialog(Customer cc, Vehicle v, Card crd)
    {
        // super(parent, modal);
        em = DBManager.em;

        custFromEditClientFrom = cc;
        vehicleFromEditClientForm = v;
        cardFromEditClientForm = crd;

        if (!em.getTransaction().isActive())
          {
            em.getTransaction().begin();
          }
        initComponents();

        ownerLBL.setText(custFromEditClientFrom.getFName() + " " + custFromEditClientFrom.getLName());
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cardGUIObject = cardFromEditClientForm;
        customerGUI = custFromEditClientFrom;
        model.Vehicle vehicleGUIObject = vehicleFromEditClientForm;
        vehicleCategoryQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT v FROM VehicleCategory v");
        vehicleCategoryList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : vehicleCategoryQuery.getResultList();
        categoryCBR = new avenue.CategoryBoxRenderer();
        markaQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("select pin from Marka pin");
        markaLIST = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(markaQuery.getResultList());
        markaCBR = new renderers.markaComboBoxRenderer();
        coloursQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT c FROM Colours c");
        coloursList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(coloursQuery.getResultList());
        colourCBR = new renderers.colourComboBoxRenderer();
        query1 = java.beans.Beans.isDesignTime() ? null : em.createQuery("Select p From Program p");
        TelePassPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("TelePassPU").createEntityManager();
        programQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("SELECT p FROM Program p");
        programList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : programQuery.getResultList();
        programCBR = new renderers.programComboBoxRenderer();
        categoryCB = new javax.swing.JComboBox();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        regNumTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cardIDTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        modellbl = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        ownerLBL = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        markaCB = new javax.swing.JComboBox();
        colourCB = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        initialCardBalanceTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        programCB = new javax.swing.JComboBox();

        categoryCBR.setText("categoryBoxRenderer1");

        markaCBR.setText("markaComboBoxRenderer1");

        colourCBR.setText("colourComboBoxRenderer1");

        programCBR.setText("programComboBoxRenderer1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        categoryCB.setRenderer(categoryCBR);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleCategoryList, categoryCB);
        bindingGroup.addBinding(jComboBoxBinding);
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleGUIObject, org.jdesktop.beansbinding.ELProperty.create("${categoryId}"), categoryCB, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("Αποθήκευση");
        saveButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Ακυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                cancelButtonActionPerformed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, vehicleGUIObject, org.jdesktop.beansbinding.ELProperty.create("${regNumber}"), regNumTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel1.setText("Αρ. Πινακίδας:  ");

        jLabel3.setText("Κατηγορία :  ");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cardGUIObject, org.jdesktop.beansbinding.ELProperty.create("${barcode}"), cardIDTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("Αρ. Συσκευής-Κάρτας:  ");

        jLabel5.setText("Χρώμα: ");

        modellbl.setText("Μάρκα:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setText("Διαχείριση Οχήματος");

        ownerLBL.setText("name");
        ownerLBL.setToolTipText("");

        markaCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yugo", "DCV", "LADA", "WV", "Trabant", "Honda", "Yamaha" }));
        markaCB.setRenderer(markaCBR);

        colourCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Κοκκινο", "Ασπρο", "Μαυρο", "Καταμαυρο", "Kεραμιδοκανελοκοραλλοκόκκινο", "Sοκοραλλί - (σοκολατί - κοραλλί)" }));
        colourCB.setRenderer(colourCBR);

        jLabel4.setText("Ιδιοκτήτης:");
        jLabel4.setToolTipText("");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cardGUIObject, org.jdesktop.beansbinding.ELProperty.create("${balance}"), initialCardBalanceTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel6.setText("Αρχικό ποσό Κάρτας:");

        jLabel8.setText("Προγραμμα Κάρτας:");
        jLabel8.setToolTipText("");

        programCB.setRenderer(programCBR);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, programList, programCB);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cardGUIObject, org.jdesktop.beansbinding.ELProperty.create("${programId}"), programCB, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(modellbl)
                                            .addComponent(jLabel5))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(markaCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(colourCB, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel1)
                                                    .addComponent(jLabel2)
                                                    .addComponent(jLabel6)
                                                    .addComponent(jLabel8))
                                                .addGap(34, 34, 34))
                                            .addGroup(layout.createSequentialGroup()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(jLabel4)
                                                    .addComponent(jLabel3))
                                                .addGap(86, 86, 86)))
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(ownerLBL)
                                            .addComponent(regNumTextField)
                                            .addComponent(categoryCB, 0, 123, Short.MAX_VALUE)
                                            .addComponent(cardIDTextField)
                                            .addComponent(initialCardBalanceTextField)
                                            .addComponent(programCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(saveButton)
                                .addGap(18, 18, 18)
                                .addComponent(cancelButton)))
                        .addGap(0, 97, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jSeparator2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ownerLBL)
                            .addComponent(jLabel4))
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(categoryCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(regNumTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cardIDTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(initialCardBalanceTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(programCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(markaCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modellbl))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(colourCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        // TODO add your handling code here:

        /// edo prepi  na ginei kai dimiopurgia kartas
        
        
     //   cardFromEditClientForm.setBarcode(Integer.parseInt(cardIDTextField.getText()));
        
        vehicleFromEditClientForm.setColour(colourCB.getSelectedItem().toString());
        vehicleFromEditClientForm.setModelType(markaCB.getSelectedItem().toString());
        
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, true);
        for (WindowListener l : this.getWindowListeners())
          {
            l.windowClosed(we);
          }
        this.setVisible(false);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED, false);
        for (WindowListener l : this.getWindowListeners())
          {
            l.windowClosed(we);
          }
        this.setVisible(false);
        //vehicleFromEditClientForm = null;
        // dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
          {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
              {
                if ("Nimbus".equals(info.getName()))
                  {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                  }
              }
          }
        catch (ClassNotFoundException ex)
          {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        catch (InstantiationException ex)
          {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        catch (IllegalAccessException ex)
          {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
          {
            java.util.logging.Logger.getLogger(VehicleDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                VehicleDialog dialog = new VehicleDialog(new Vehicle());
                dialog.addWindowListener(new java.awt.event.WindowAdapter()
                {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e)
                    {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager TelePassPUEntityManager;
    private javax.swing.JButton cancelButton;
    private model.Card cardGUIObject;
    private javax.swing.JTextField cardIDTextField;
    private javax.swing.JComboBox categoryCB;
    private avenue.CategoryBoxRenderer categoryCBR;
    private javax.swing.JComboBox colourCB;
    private renderers.colourComboBoxRenderer colourCBR;
    private java.util.List<model.Colours> coloursList;
    private javax.persistence.Query coloursQuery;
    private model.Customer customerGUI;
    private javax.swing.JTextField initialCardBalanceTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JComboBox markaCB;
    private renderers.markaComboBoxRenderer markaCBR;
    private java.util.List<Marka> markaLIST;
    private javax.persistence.Query markaQuery;
    private javax.swing.JLabel modellbl;
    private javax.swing.JLabel ownerLBL;
    private javax.swing.JComboBox programCB;
    private renderers.programComboBoxRenderer programCBR;
    private java.util.List<model.Program> programList;
    private javax.persistence.Query programQuery;
    private javax.persistence.Query query1;
    private javax.swing.JTextField regNumTextField;
    private javax.swing.JButton saveButton;
    private java.util.List<model.VehicleCategory> vehicleCategoryList;
    private javax.persistence.Query vehicleCategoryQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
